﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using GMS;

namespace GMS
{
    public partial class Pages_EstimatorHome : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (SessionVariable.Instance.UserDetails.SelectedStoneTypeId > 0)
                {
                    dvMain.Visible = true;
                    LoadShapes();
                }
                else
                {
                    dvMain.Visible = false;
                    ShowMessage("Please select a surface first.");
                }
            }
        }

        private void LoadShapes()
        {
            rptrSelectShape.DataSource = BLL.Common.GetShapes();
            rptrSelectShape.DataBind();
        }

        protected void rptrSelectShape_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ImageButton imgShape = (ImageButton)e.Item.FindControl("imgShape");

                using (HtmlGenericControl dvImg = (HtmlGenericControl)e.Item.FindControl("dvImg"))
                {
                    if (dvImg != null)
                    {
                        dvImg.Attributes["onmouseover"] = "this.style.borderColor='Red'";
                        dvImg.Attributes["onmouseout"] = "this.style.borderColor='transparent'";
                        if (imgShape.ToolTip.Contains("7. Rectangular Island"))
                        {
                            imgShape.Height = Unit.Pixel(80);
                        }
                    }
                }

                if (!string.IsNullOrEmpty(imgShape.ToolTip))
                {
                    char[] toolTip = imgShape.ToolTip.ToCharArray();
                    int shapeID = Convert.ToInt32(toolTip[0].ToString());
                    if (shapeID == SessionVariable.Instance.UserDetails.ShapeId)
                    {
                        using (HtmlGenericControl dvImg = (HtmlGenericControl)e.Item.FindControl("dvImg"))
                        {
                            if (dvImg != null)
                            {
                                dvImg.Style.Add(HtmlTextWriterStyle.BorderColor, "Green");
                                dvImg.Style.Add(HtmlTextWriterStyle.BorderWidth, "1px");
                            }
                        }
                    }
                }
            }
        }

        protected void rptrSelectShape_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) &&
                string.Compare(e.CommandName.ToLower(), "click") == 0)
            {
                SessionVariable.Instance.UserDetails.ShapeId = Convert.ToInt32(e.CommandArgument);

                using (Repeater rptr = (Repeater)e.Item.Parent)
                {
                    if (rptr != null)
                    {
                        foreach (RepeaterItem item in rptr.Items)
                        {
                            using (HtmlGenericControl dvImg = (HtmlGenericControl)item.FindControl("dvImg"))
                            {
                                if (dvImg != null)
                                {
                                    dvImg.Style.Add(HtmlTextWriterStyle.BorderColor, "transparent");
                                }
                            }
                        }
                    }
                }

                using (HtmlGenericControl dvImg = (HtmlGenericControl)e.Item.FindControl("dvImg"))
                {
                    if (dvImg != null)
                    {
                        ImageButton imgShape = (ImageButton)e.Item.FindControl("imgShape");
                        dvImg.Style.Add(HtmlTextWriterStyle.BorderColor, "Green");
                        dvImg.Style.Add(HtmlTextWriterStyle.BorderWidth, "4px");
                        char[] toolTip = imgShape.ToolTip.ToCharArray();
                        SessionVariable.Instance.UserDetails.ShapeId = Convert.ToInt32(toolTip[0].ToString());
                        SessionVariable.Instance.UserDetails.SelectedShapeUrl = imgShape.ImageUrl;
                        SessionVariable.Instance.UserDetails.SelectedShapeEnterInputsUrl = imgShape.AlternateText;
                        nextPage();
                    }
                }

            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("SelectSurface.aspx");
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            nextPage();
        }

        private void nextPage()
        {
            if (SessionVariable.Instance.UserDetails.IsDataPersisted)
            {
                Response.Redirect("Estimate.aspx");
            }
            else
            {
                if (SessionVariable.Instance.UserDetails.ShapeId > 0 &&
                    !string.IsNullOrEmpty(SessionVariable.Instance.UserDetails.SelectedShapeUrl))
                {
                    Response.Redirect("EnterShapeInputs.aspx");
                }
                else
                {
                    ShowMessage("Please select a shape.");
                }
            }
        }
        private void ShowMessage(string msgText)
        {
            HtmlGenericControl divMsg = (HtmlGenericControl)Page.Master.FindControl("divMsg");

            if (divMsg != null)
            {
                Control msg = this.Page.LoadControl("Controls/Message.ascx");

                if (msg != null)
                {
                    Label lblMessage = (Label)msg.FindControl("lblMessage");

                    if (lblMessage != null)
                    {
                        lblMessage.Text = msgText;
                    }

                    divMsg.Controls.Add(msg);
                }
            }

        }
    }
}